package com.sw.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import net.sf.json.JSONArray;




/**
 * 发送短信验证码工具类
 * @author Administrator
 *
 */
public class SendSMSUtil {

	/**
	 * 发送短信验证码方法 
	 * @param phone 参数一：需要发送的手机号码
	 * @param templateId 参数二： 短信模板ID
	 * @return 返回是否发送成功！
	 */
	public static boolean sendSMSValidataCode(String phone,String templateId,int mobile_code){
		boolean succeed = false;
		String callerId = "51518000009";
		String password = "72d0b17a765ab958525a809d6dced8ca";
		String getURL = "http://120.197.89.93:80/EOPS1.0/captcha/get_01/"+phone+";callerId="+callerId+";" 
					+"password="+password+";templateId="+templateId+";param="+mobile_code;
		try {
				URL getUrl = new URL(getURL);
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				//执行
				connection.connect();
				String result = "";
				//读取字符流 获取响应结果
				BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码 
				String lines  = ""; 
				while ((lines = reader.readLine()) != null){ 
					result = new String(lines.getBytes(), "utf-8"); 
				}
				System.out.println(result);
				JSONArray array = JSONArray.fromObject(result);
				
				String  code= array.getJSONObject(0).getString("code");
				if("00".equals(code)){
					succeed = true;
				}
				reader.close();
				connection.disconnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		return succeed;
	}
}
